/*
Copyright 2014 Aaron Batilo

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

//@author: Aaron Batilo

//This file is the main of this program. All operations will begin here.

//Include a header containing all of the other header files we need
#include "main.h"

//This is the main entry point to the program
//The parameters represent the number of arguments passed in, and what those arguments are, respectively
int main(int argc, char *argv[])
{
	//Check to see if the program was executed properly
	if (argc != 2)
	{
		//If it wasn't, notify the user of the proper usage
		cout << "\nUSAGE:\nTo properly use this program, please include a single parameter during execution\nThis parameter should be the path and name to a program with a:\n-.TXT\n-.CMP file extension.\nThis program will be terminated.\n\n";
		
		//Exit with an error code
		return -1;
	}
	else
	{
		//Get the file name passed in
		string fileName = argv[1];

		//Initialize a file instance with this new file
		File file(fileName);

		//Check to see if the file passed in exists
		if (file.isExists())
		{
			//Check to see if the file is a TXT file
			if (file.getExtension() == FileType::TXT)
			{
				//Initialize a raw file
				RawFile rawFile(fileName);

				//Initialize a compressed file instance
				CompressedFile compressedFile(rawFile);
				//Call the compression method
				compressedFile.compress();

				cout << endl << fileName << " was compressed successfully\n\n";
			}
			//Otherwise, check to see if it's a CMP file
			else if (file.getExtension() == FileType::CMP)
			{
				//Initialize the key file as a general file instance
				File keyFile(file.getNameNoExtension() + ".KEY");

				//Make sure the key file exists
				if (keyFile.isExists())
				{
					//Initialize a new compressed file instance
					CompressedFile compressedFile(fileName);
					//Call the decompression method
					compressedFile.decompress();

					cout << endl << fileName << " was decompressed successfully\n\n";
				}
				//Otherwise the key file couldn't be found and the program wouldn't execute properly
				else
				{
					//Let the user know what's wrong
					cout << "\nKey file not found. This program will be terminated.\n\n";
					//Exit with an error code
					return -1;
				}
			}
			else
			{
				//Tell the user we have an unrecognized file type
				cout << "\nThe passed in file was of an unrecognized file type. This program will be terminated.\n\n";
				//Exit with an error code
				return -1;
			}
		}
		else
		{
			//Let the user know the passed in file could not be found
			cout << "\nSpecified file not found. This program will be terminated.\n\n";
			//Exit with an error code
			return -1;
		}
	}

	return 0;
}